
from whiff import resolver
from whiff import whiffenv
from whiff import gateway
import mimetypes
import os

def getContent(env, start_response):
    env = resolver.process_cgi(env, parse_cgi=True)
    filepath = whiffenv.cgiGet(env, "path")
    root = gateway.getResource(env, ["root"])
    root = os.path.abspath(root)
    assert filepath.find("..")<0, "relative paths are not permitted"
    while filepath.startswith("/"):
        filepath = filepath[1:]
    fullpath = os.path.join(root, filepath)
    content = file(fullpath, "rb").read()
    (content_type, encoding) = mimetypes.guess_type(fullpath)
    if not content_type or content_type.startswith("text") or content_type.endswith("python"):
        content_type = "text/plain"
    start_response("200 OK", [('Content-Type', content_type)])
    return [content]
    
__wsgi__ = getContent
